package com.mzj.saas.jobs.bill.service;

import com.mzj.saas.commons.enums.BusinessTypeEnum;
import com.mzj.saas.jobs.backlog.entity.BacklogItem;
import com.mzj.saas.jobs.backlog.repository.BacklogItemRepository;
import com.mzj.saas.jobs.bill.entity.*;
import com.mzj.saas.jobs.bill.repository.*;
import com.mzj.saas.jobs.lifePay.entity.EstatePaymentOrder;
import com.mzj.saas.jobs.lifePay.repository.EstatePaymentOrderRepository;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 补齐用户信息
 */
@Component
//@Service
public class SupplementUserInfoService {
    private static Logger log = LoggerFactory.getLogger(SupplementUserInfoService.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private AppUserRepository appUserRepository;//appUser表
    @Autowired
    private CommunityHouseUserRepository communityHouseUserRepository;//房产成员信息登记表明细表
    @Autowired
    private SmartLockAuthRepository smartLockAuthRepository;//智能锁用户授权明细表
    @Autowired
    private EstateExpenseBillRepository expenseBillRepository;//物业房产-其它费用账单明细
    @Autowired
    private EstatePaymentOrderRelationRepository estatePaymentOrderRelationRepository;//物业-账单与支付记录关系表
    @Autowired
    private EstatePaymentOrderRepository estatePaymentOrderRepository;//物业房产-支付记录
    @Autowired
    private MatchUserRepository matchUserRepository;//承租人库
    @Autowired
    private BacklogItemRepository backlogItemRepository;//代办事项
    @Autowired
    private EstateRentBillRepository estateRentBillRepository;//物业房产-租金/押金账单明细
    @Autowired
    private SaasComplainSuggestRepository complainSuggestRepository; // 投诉建议
    @Autowired
    private HouseRemainingRepository houseRemainingRepository;


    public String work() throws Exception {

        /**
         * 补齐一下表字段的用户ID（承租人ID）等
         * 1. saas_community_house_user -- 房产成员信息登记表明细表 补齐字段--user_id
         * 2. saas_smart_lock_auth -- 智能锁用户授权明细表 补齐字段--match_user_id
         * 3. saas_estate_expense_bill -- 物业房产-其它费用账单明细  补齐字段--match_user_id
         * 4. saas_estate_payment_order -- 物业房产-支付记录  补齐字段--pay_user_id
         * 5. m_match_user_stock -- 承租人库   补齐字段--match_user_id
         * 6.saas_backlog_item -- 待办事项 补齐字段-- match_user_id   phone
         * 7.saas_estate_rent_bill -- 物业房产-租金/押金账单明细   补齐字段--match_user_id
         */
        log.info("补齐用户信息  start");
        // 获取 appUser表状态为 1（启用）的数据
        List<AppUser> appUserList = appUserRepository.findByStatus(1);

        //获得用户的手机号和ID
        Map<String, String> appUserMap = appUserList.stream().collect(Collectors.toMap(AppUser::getPhone, AppUser::getId));


        //补齐用户信息
        // SupplementUser();

        // 1.补齐 saas_community_house_user -- 房产成员信息登记表明细表 补齐字段--user_id
        SupplementHouseUser(appUserMap);

        //2.补齐 saas_smart_lock_auth -- 智能锁用户授权明细表 补齐字段--match_user_id
        SupplementLockAuth(appUserList);

        //3.补齐 saas_estate_expense_bill ---- 物业房产-其它费用账单明细  补齐字段--match_user_id
        SupplementExpenseBill(appUserMap);

        //4.补齐 ssaas_estate_payment_order -- 物业房产-支付记录  补齐字段--pay_user_id
        SupplementPaymentOrder(appUserMap);

        //5.补齐  m_match_user_stock -- 承租人库   补齐字段--match_user_id
        SupplementUserStock(appUserMap);

        //6.补齐  saas_backlog_item --待办事项
        SupplementBacklogItem(appUserMap);

        //7.saas_estate_rent_bill -- 物业房产-租金/押金账单明细   补齐字段--match_user_id
        SupplementRentBill(appUserMap);


        //8.saas_complain_suggest -- 投诉建议   补齐字段--user_name
        ComplainSuggest(appUserList);

        //9.saas_house_remaining --余额账户  补齐字段--match_user_id
        SupplementRemaining(appUserMap);
        log.info("补齐用户信息  end");

        return "SUCCESS";
    }

    /**
     * 9.saas_house_remaining --余额账户  补齐字段--match_user_id
     */
    private void SupplementRemaining(Map<String, String> appUserMap) {
        try {
            log.info("补齐 余额账户 (saas_house_remaining)  start");
            log.info("更新 余额账户 (saas_house_remaining)");
            List<HouseRemaining> houseRemainings = houseRemainingRepository.findByMatchUserIdIsNull();
            if (!houseRemainings.isEmpty()){
                for (HouseRemaining houseRemaining : houseRemainings) {

                    List<CommunityHouseUser> houseUserList = communityHouseUserRepository.findByCommunityHouseIdAndStatus(houseRemaining.getHouseId(), 1);
                    for (CommunityHouseUser communityHouseUser : houseUserList) {
                        if (communityHouseUser.getType() == 1 && StringUtils.isEmpty(houseRemaining.getMatchUserId())) {
                            houseRemaining.setMatchUserId(communityHouseUser.getUserId());
                        }
                    }
                }
                houseRemainingRepository.saveAll(houseRemainings);

            }




            log.info("补齐  余额账户 (saas_house_remaining) end");
        } catch (Exception e) {
            log.error("补齐  余额账户 (saas_house_remaining)信息异常  " + e.getMessage(), e);
        }

    }

    /**
     * 1.补齐saas_community_house_user -- 房产成员信息登记表明细表 补齐字段--user_id
     */
    public void SupplementHouseUser(Map<String, String> appUserMap) throws Exception {
        try {
            log.info("补齐  房产成员信息登记表明细表(saas_community_house_user)  start");
            //获得房产成员信息登记表明细表 状态为正常的数据
            List<CommunityHouseUser> communityHouseUserList = communityHouseUserRepository.findByStatus(1);
            //循环遍历
            for (CommunityHouseUser communityHouseUser : communityHouseUserList) {

                //如果成员手机号为NULL，则跳过
                if (StringUtils.isEmpty(communityHouseUser.getMobile())) {
                    continue;
                }

                //如果不等于NULL，则根据手机号获取   appUserMap （用户表）的ID
                String appUserId = appUserMap.get(communityHouseUser.getMobile());

                //获取的手机号等于NULL ，则跳过
                if (StringUtils.isEmpty(appUserId)) {
                    continue;
                }
                //如果 appUserId 不为空，则写入 房产成员信息登记表明细表的 User_iD
                communityHouseUser.setUserId(appUserId);

            }
            //更新 房产成员信息登记表明细表
            communityHouseUserRepository.saveAll(communityHouseUserList);

            log.info("更新 房产成员信息登记表明细表(saas_community_house_user)");

            log.info("补齐  房产成员信息登记表明细表(saas_community_house_user)  end");

        } catch (Exception e) {
            log.error("补齐  房产成员信息登记表明细表(saas_community_house_user)信息异常  " + e.getMessage(), e);
        }
    }


    /**
     * 2.补齐 saas_smart_lock_auth -- 智能锁用户授权明细表 补齐字段--match_user_id
     */
    public void SupplementLockAuth(List<AppUser> appUserList) throws Exception {
        try {
            log.info("补齐  智能锁用户授权明细表(saas_smart_lock_auth)  start");

            //获得用户的手机号和ID
            Map<String, List<AppUser>> appUserMap = appUserList.stream().collect(Collectors.groupingBy(AppUser::getPhone));


            //获得智能锁用户授权明细表全部数据
            Iterable<SmartLockAuth> smartLockAuthList = smartLockAuthRepository.findAll();
            //循环遍历
            for (SmartLockAuth smartLockAuth : smartLockAuthList) {
                //如果成员手机号为NULL，则跳过
                if (StringUtils.isEmpty(smartLockAuth.getMobile())) {
                    continue;
                }

                //如果不等于NULL，则根据手机号获取   appUserMap （用户表）的ID
                List<AppUser> userList = appUserMap.get(smartLockAuth.getMobile());

                //获取的手机号等于NULL ，则跳过
                if (userList == null || userList.size() <= 0) {
                    continue;
                }

                //根据房屋ID获得承租人ID
                List<CommunityHouseUser> houseUserList = communityHouseUserRepository.findByCommunityHouseIdAndStatus(smartLockAuth.getHouseId(), 1);
                for (CommunityHouseUser communityHouseUser : houseUserList) {
                    if (communityHouseUser.getType() == 1 && StringUtils.isEmpty(smartLockAuth.getMatchUserId())) {
                        smartLockAuth.setMatchUserId(communityHouseUser.getUserId());
                    }
                    Map<String, List<CommunityHouseUser>> collect = houseUserList.stream().collect(Collectors.groupingBy(CommunityHouseUser::getMobile));
                    List<CommunityHouseUser> communityHouseUsers = collect.get(smartLockAuth.getMobile());

                    if (communityHouseUsers != null && communityHouseUsers.size() > 0) {
                        smartLockAuth.setUserName(communityHouseUsers.get(0).getName());
                    }


                }


            }
            //更新 智能锁用户授权明细表
            smartLockAuthRepository.saveAll(smartLockAuthList);

            log.info("更新 智能锁用户授权明细表(saas_smart_lock_auth)");

            log.info("补齐  智能锁用户授权明细表(saas_smart_lock_auth)  end");


        } catch (Exception e) {
            log.error("补齐  智能锁用户授权明细表(saas_smart_lock_auth)信息异常  " + e.getMessage(), e);
        }
    }

    /**
     * 3.补齐 saas_estate_expense_bill ---- 物业房产-其它费用账单明细  补齐字段--match_user_id
     */
    public void SupplementExpenseBill(Map<String, String> appUserMap) throws Exception {
        try {
            log.info("补齐  物业房产-其它费用账单明细(saas_estate_expense_bill)  start");

            //获得 全部物业房产-其它费用账单明细 的房屋和承租人手机号
            String expenseBillSql = " select b.mobile as mobile ,b.community_house_id as communityHouseId from  saas_house a\n" +
                    "LEFT JOIN saas_community_house_user b on b.community_house_id = a.id \n" +
                    "where a.id in(select house_id from  saas_estate_expense_bill) and b.status=1 and b.type=1 and b.mobile is not null  ";
            List<CommunityHouseUser> houseUserList = jdbcTemplate.query(expenseBillSql, new BeanPropertyRowMapper<>(CommunityHouseUser.class));

            //获得当前房屋承租人手机号
            Map<String, String> houseUserMap = houseUserList.stream().collect(Collectors.toMap(CommunityHouseUser::getCommunityHouseId, CommunityHouseUser::getMobile));

            //全部物业房产-其它费用账单明细
            List<EstateExpenseBill> estateExpenseBillList = expenseBillRepository.findAll();
            //循环遍历
            for (EstateExpenseBill estateExpenseBill : estateExpenseBillList) {
                //根据房屋ID，获得当前房屋承租人手机号
                String mobile = houseUserMap.get(estateExpenseBill.getHouseId());
                //如果获取不到，则跳过
                if (StringUtils.isEmpty(mobile)) {
                    continue;
                }
                //如果承租人id不为空，则跳过
                if (StringUtils.isNotEmpty(estateExpenseBill.getMatchUserId())) {
                    continue;
                }
                //如果不等于NULL，则根据手机号获取   appUserMap （用户表）的ID
                String appUserId = appUserMap.get(mobile);

                //获取的手机号等于NULL ，则跳过
                if (StringUtils.isEmpty(appUserId)) {
                    continue;
                }

                estateExpenseBill.setMatchUserId(appUserId);
            }

            //更新物业房产-其它费用账单明细
            expenseBillRepository.saveAll(estateExpenseBillList);

            log.info("更新 物业房产-其它费用账单明细(saas_estate_expense_bill)");

            log.info("补齐  物业房产-其它费用账单明细(saas_estate_expense_bill)  end");

        } catch (Exception e) {
            log.error("补齐  物业房产-其它费用账单明细(saas_estate_expense_bill)信息异常  " + e.getMessage(), e);
        }
    }


    /**
     * 4.补齐 ssaas_estate_payment_order -- 物业房产-支付记录  补齐字段--pay_user_id
     */
    public void SupplementPaymentOrder(Map<String, String> appUserMap) throws Exception {
        try {
            log.info("补齐 物业房产-支付记录(saas_estate_payment_order)  start");

            //全部物业房产-其它费用账单明细
            List<EstateExpenseBill> estateExpenseBillList = expenseBillRepository.findAll();

            //补齐 物业房产-支付记录
            for (EstateExpenseBill estateExpenseBill : estateExpenseBillList) {
                //支付状态不等于已缴费则跳过
                if (estateExpenseBill.getPaymentStatus() != 1 || StringUtils.isNotEmpty(estateExpenseBill.getMatchUserId())) {
                    continue;
                }

                Integer type = chargeType(estateExpenseBill.getChargeType());
                EstatePaymentOrderRelation orderRelation = estatePaymentOrderRelationRepository.findByBillIdAndBusinessType(estateExpenseBill.getId(), type);

                if (orderRelation == null) {
                    continue;
                }
                EstatePaymentOrder estatePaymentOrder = estatePaymentOrderRepository.findByOrderNumber(orderRelation.getOrderNumber());
                if (StringUtils.isEmpty(estatePaymentOrder.getPayUserId())) {
                    estatePaymentOrder.setPayUserId(estateExpenseBill.getMatchUserId());
                    estatePaymentOrderRepository.save(estatePaymentOrder);
                }

            }
            log.info("更新 物业房产-支付记录(saas_estate_payment_order)");

            log.info("补齐 物业房产-支付记录(saas_estate_payment_order)  end");


        } catch (Exception e) {
            log.error("补齐  物业房产-支付记录(saas_estate_payment_order)信息异常  " + e.getMessage(), e);
        }
    }


    /**
     * 5.补齐  m_match_user_stock -- 承租人库   补齐字段--match_user_id
     */
    public void SupplementUserStock(Map<String, String> appUserMap) throws Exception {
        try {
            log.info("补齐 承租人库(m_match_user_stock)  start");

            //获得未删除的承租人数据
            List<MatchUser> matchUserList = matchUserRepository.findByDeleteStatus(false);

            for (MatchUser matchUser : matchUserList) {
                if (StringUtils.isEmpty(matchUser.getPhone()) || StringUtils.isNotEmpty(matchUser.getMatchUserId())) {
                    continue;
                }
                //如果不等于NULL，则根据手机号获取   appUserMap （用户表）的ID
                String appUserId = appUserMap.get(matchUser.getPhone());

                //获取的手机号等于NULL ，则跳过
                if (StringUtils.isEmpty(appUserId)) {
                    continue;
                }
                matchUser.setMatchUserId(appUserId);

            }
            matchUserRepository.saveAll(matchUserList);

            log.info("更新 承租人库(m_match_user_stock)");

            log.info("补齐 承租人库(m_match_user_stock)  end");


        } catch (Exception e) {
            log.error("补齐  承租人库(m_match_user_stock)信息异常  " + e.getMessage(), e);
        }
    }


    /**
     * 6.补齐  saas_backlog_item --待办事项
     */
    public void SupplementBacklogItem(Map<String, String> appUserMap) throws Exception {
        try {
            log.info("补齐 待办事项   (saas_backlog_item)  start");
            //获得 承租人（接收人） 字段等于空 ， 手机号不等于空 的待办事项
            List<BacklogItem> backlogItemList = backlogItemRepository.findByMatchUserIdAndPhone(null, null);

            for (BacklogItem backlogItem : backlogItemList) {
                String appUserId = appUserMap.get(backlogItem.getPhone());
                if (StringUtils.isNotEmpty(appUserId)) {
                    backlogItem.setMatchUserId(appUserId);
                }

            }
            backlogItemRepository.saveAll(backlogItemList);

            //获得待办事项Map
            // Map<Long, BacklogItem> itemMap = backlogItemList.stream().collect(Collectors.toMap(BacklogItem::getId, x -> x));


            log.info("更新 待办事项(saas_backlog_item) ");

            log.info("补齐  待办事项表（saas_backlog_item） end");
        } catch (Exception e) {
            log.error("补齐   待办事项表（saas_backlog_item）信息异常  " + e.getMessage(), e);
        }
    }


    /**
     * 7.saas_estate_rent_bill -- 物业房产-租金/押金账单明细   补齐字段--match_user_id
     */
    public void SupplementRentBill(Map<String, String> appUserMap) throws Exception {
        try {
            log.info("补齐 物业房产-租金/押金账单明细 (saas_estate_rent_bill)  start");

            //全部物业房产-租金/押金 承租人等于空的数据
            String sql = " SELECT * FROM saas_estate_rent_bill where match_user_id is null or match_user_id='' ";
            List<EstateRentBill> estateRentBillList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(EstateRentBill.class));

            for (EstateRentBill estateRentBill : estateRentBillList) {
                if (StringUtils.isNotEmpty(estateRentBill.getMatchUserId())) {
                    continue;
                }
                List<CommunityHouseUser> communityHouseUserList = communityHouseUserRepository.findByCommunityHouseIdAndStatusAndType(estateRentBill.getHouseId(), 1, 1);
                if (null == communityHouseUserList || communityHouseUserList.size() <= 0) {
                    continue;
                }
                if (StringUtils.isNotEmpty(communityHouseUserList.get(0).getUserId())) {
                    estateRentBill.setMatchUserId(communityHouseUserList.get(0).getUserId());
                    estateRentBillRepository.save(estateRentBill);
                }
            }


            log.info("更新 物业房产-租金/押金账单明细 (saas_estate_rent_bill) ");

            log.info("补齐 物业房产-租金/押金账单明细 (saas_estate_rent_bill)  end");

        } catch (Exception e) {
            log.error("补齐 物业房产-租金/押金账单明细 (saas_estate_rent_bill) 信息异常  " + e.getMessage(), e);
        }
    }


    /**
     * 8.saas_complain_suggest -- 投诉建议   补齐字段--user_name
     */
    public void ComplainSuggest(List<AppUser> appUserList) throws Exception {
        try {
            log.info("补齐 投诉建议 (saas_complain_suggest)  start");
            //获得用户的手机号和ID
            Map<String, List<AppUser>> appUserMap = appUserList.stream().collect(Collectors.groupingBy(AppUser::getId));

            List<SaasComplainSuggest> complainSuggestList = complainSuggestRepository.findAll();

            ArrayList<SaasComplainSuggest> list = new ArrayList<>();

            for (SaasComplainSuggest suggest : complainSuggestList) {
                if (StringUtils.isBlank(suggest.getUserId())) {
                    continue;
                }
                List<AppUser> userList = appUserMap.get(suggest.getUserId());
                if (userList != null && userList.size() > 0) {
                    suggest.setUserName(userList.get(0).getName());
                    list.add(suggest);
                }
            }
            complainSuggestRepository.saveAll(list);


            log.info("补齐 投诉建议 (saas_complain_suggest)  end");

        } catch (Exception e) {
            log.error("补齐 投诉建议 (saas_complain_suggest) 信息异常  " + e.getMessage(), e);
        }
    }


    public void SupplementUser() throws Exception {
        try {
            log.info("补齐用户信息  start");
            // 获取 appUser表状态为 1（启用）的数据
            List<AppUser> appUserList = appUserRepository.findByStatus(1);

            //获得用户的手机号和ID
            Map<String, String> appUserMap = appUserList.stream().collect(Collectors.toMap(AppUser::getPhone, AppUser::getId));

            //获得房产成员信息登记表明细表 状态为正常的数据
            List<CommunityHouseUser> communityHouseUserList = communityHouseUserRepository.findByStatus(1);

            //循环遍历
            for (CommunityHouseUser communityHouseUser : communityHouseUserList) {

                //如果成员手机号为NULL，则跳过
                if (StringUtils.isEmpty(communityHouseUser.getMobile())) {
                    continue;
                }

                //如果不等于NULL，则根据手机号获取   appUserMap （用户表）的ID
                String appUserId = appUserMap.get(communityHouseUser.getMobile());

                //获取的手机号等于NULL ，则跳过
                if (StringUtils.isEmpty(appUserId)) {
                    continue;
                }
                //如果 appUserId 不为空，则写入 房产成员信息登记表明细表的 User_iD
                communityHouseUser.setUserId(appUserId);

            }
            //更新 房产成员信息登记表明细表
            communityHouseUserRepository.saveAll(communityHouseUserList);

            log.info("更新 房产成员信息登记表明细表(saas_community_house_user)");

            //获得智能锁用户授权明细表全部数据
            Iterable<SmartLockAuth> smartLockAuthList = smartLockAuthRepository.findAll();
            //循环遍历
            for (SmartLockAuth smartLockAuth : smartLockAuthList) {
                //如果成员手机号为NULL，则跳过
                if (StringUtils.isEmpty(smartLockAuth.getMobile())) {
                    continue;
                }

                //如果不等于NULL，则根据手机号获取   appUserMap （用户表）的ID
                String appUserId = appUserMap.get(smartLockAuth.getMobile());

                //获取的手机号等于NULL ，则跳过
                if (StringUtils.isEmpty(appUserId)) {
                    continue;
                }

                if (StringUtils.isEmpty(smartLockAuth.getMatchUserId())) {
                    //根据房屋ID获得承租人ID
                    List<CommunityHouseUser> houseUserList = communityHouseUserRepository.findByCommunityHouseIdAndStatusAndType(smartLockAuth.getHouseId(), 1, 1);
                    if (houseUserList != null && houseUserList.size() > 0) {
                        smartLockAuth.setMatchUserId(houseUserList.get(0).getUserId());
                    }
                }

                smartLockAuth.setAuthorUserId(appUserId);

            }
            //更新 智能锁用户授权明细表
            smartLockAuthRepository.saveAll(smartLockAuthList);

            log.info("更新 智能锁用户授权明细表(saas_smart_lock_auth)");

            //获得 全部物业房产-其它费用账单明细 的房屋和承租人手机号
            String expenseBillSql = " select b.mobile as mobile ,b.community_house_id as communityHouseId from  saas_house a\n" +
                    "LEFT JOIN saas_community_house_user b on b.community_house_id = a.id \n" +
                    "where a.id in(select house_id from  saas_estate_expense_bill) and b.status=1 and b.type=1 and b.mobile is not null  ";
            List<CommunityHouseUser> houseUserList = jdbcTemplate.query(expenseBillSql, new BeanPropertyRowMapper<>(CommunityHouseUser.class));

            //获得当前房屋承租人手机号
            Map<String, String> houseUserMap = houseUserList.stream().collect(Collectors.toMap(CommunityHouseUser::getCommunityHouseId, CommunityHouseUser::getMobile));

            //全部物业房产-其它费用账单明细
            List<EstateExpenseBill> estateExpenseBillList = expenseBillRepository.findAll();
            //循环遍历
            for (EstateExpenseBill estateExpenseBill : estateExpenseBillList) {
                //根据房屋ID，获得当前房屋承租人手机号
                String mobile = houseUserMap.get(estateExpenseBill.getHouseId());
                //如果获取不到，则跳过
                if (StringUtils.isEmpty(mobile)) {
                    continue;
                }

                //如果不等于NULL，则根据手机号获取   appUserMap （用户表）的ID
                String appUserId = appUserMap.get(mobile);

                //获取的手机号等于NULL ，则跳过
                if (StringUtils.isEmpty(appUserId)) {
                    continue;
                }
                estateExpenseBill.setMatchUserId(appUserId);
            }

            //更新物业房产-其它费用账单明细
            expenseBillRepository.saveAll(estateExpenseBillList);

            log.info("更新 物业房产-其它费用账单明细(saas_estate_expense_bill)");


            //补齐 物业房产-支付记录
            for (EstateExpenseBill estateExpenseBill : estateExpenseBillList) {
                //支付状态不等于已缴费则跳过
                if (estateExpenseBill.getPaymentStatus() != 1 || StringUtils.isEmpty(estateExpenseBill.getMatchUserId())) {
                    continue;
                }

                Integer type = chargeType(estateExpenseBill.getChargeType());
                EstatePaymentOrderRelation orderRelation = estatePaymentOrderRelationRepository.findByBillIdAndBusinessType(estateExpenseBill.getId(), type);

                if (orderRelation == null) {
                    continue;
                }
                EstatePaymentOrder estatePaymentOrder = estatePaymentOrderRepository.findByOrderNumber(orderRelation.getOrderNumber());
                if (StringUtils.isEmpty(estatePaymentOrder.getPayUserId())) {
                    estatePaymentOrder.setPayUserId(estateExpenseBill.getMatchUserId());
                    estatePaymentOrderRepository.save(estatePaymentOrder);
                }

            }
            log.info("更新 物业房产-支付记录(saas_estate_payment_order)");

            //获得未删除的承租人数据
            List<MatchUser> matchUserList = matchUserRepository.findByDeleteStatus(false);

            for (MatchUser matchUser : matchUserList) {
                if (StringUtils.isEmpty(matchUser.getPhone()) || StringUtils.isNotEmpty(matchUser.getMatchUserId())) {
                    continue;
                }
                //如果不等于NULL，则根据手机号获取   appUserMap （用户表）的ID
                String appUserId = appUserMap.get(matchUser.getPhone());

                //获取的手机号等于NULL ，则跳过
                if (StringUtils.isEmpty(appUserId)) {
                    continue;
                }
                matchUser.setMatchUserId(appUserId);

            }
            matchUserRepository.saveAll(matchUserList);

            log.info("更新 承租人库(m_match_user_stock)");

            log.info("补齐用户信息  end");

        } catch (Exception e) {
            log.error("补齐用户信息异常  " + e.getMessage(), e);
        }
    }

    public Integer chargeType(Integer chargeType) {
        Integer type = null;
        if (chargeType == 1) {
            type = BusinessTypeEnum.WATER_FEE.getValue();

        } else if (chargeType == 2) {
            type = BusinessTypeEnum.ELECTRIC_FEE.getValue();

        } else if (chargeType == 3) {
            type = BusinessTypeEnum.PROPERTY_FEE.getValue();

        } else if (chargeType == 4) {
            type = BusinessTypeEnum.BROADBAND_FEE.getValue();

        } else if (chargeType == 5) {
            type = BusinessTypeEnum.TV_FEE.getValue();

        } else if (chargeType == 6) {
            type = BusinessTypeEnum.COALGAS_FEE.getValue();

        } else if (chargeType == 7) {
            type = BusinessTypeEnum.HEATING_FEE.getValue();

        } else if (chargeType == 8) {
            type = BusinessTypeEnum.RENT_FEE.getValue();

        } else if (chargeType == 9) {
            type = BusinessTypeEnum.DEPOSIT_FEE.getValue();

        } else if (chargeType == 10) {
            type = BusinessTypeEnum.OTHER_FEE.getValue();

        }
        return type;
    }


}
